package com.jackrain.nea.core.mapper;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.entity.AdColumnModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Mapper
@Component
public interface AdColumnMapper {
    @Select("SELECT count(1) from ad_column where ref_column_id = #{refColumnId}")
    int countWithRefColumn(long refColumnId);

    @Select("select count(*) from ad_column where ad_table_id = #{tableId} and id = #{columnId}")
    int count(@Param("tableId") long tableId, @Param("columnId") long columnId);

    @Select("select REF_COLUMN_ID from ad_column where id = #{columnId}")
    String refColumnId(long columnId);

    @Select("select count(1) from ad_column where id = #{columnId} and exists (select 1 from ad_LIMITVALUE_GROUP where ad_LIMITVALUE_GROUP.id = ad_column.ad_LIMITVALUE_GROUP_id and ad_LIMITVALUE_GROUP.name = 'YESNO')")
    int columnCountWithYesNoGroup(long columnId);

    @Delete("delete from ad_column where ad_table_id = #{tableId}")
    int deleteWithTable(long adTableId);

    @Delete("delete from ad_column where id = #{id}")
    int deleteWithid(long id);

    @Update("update ad_column set name = #{name},dbname = #{dbname},nullable=#{nullable},modifiable = #{modifiable} where id = #{columnId}")
    int updateName(@Param("columnId") long columnId, @Param("name") String name, @Param("dbname") String dbname, @Param("nullable") String nullable, @Param("modifiable") String modifiable);

    @Update("update ad_column set isak = 'N' where ad_table_id = #{tableId} and id != #{columnId} and isak = 'Y'")
    int updateAK(@Param("tableId") long tableId, @Param("columnId") long columnId);

    @Update("update ad_column set isdk = 'N' where ad_table_id = #{tableId}  and id != #{columnId} and isdk = 'Y'")
    int updateDK(@Param("tableId") long tableId, @Param("columnId") long columnId);

    @Select("select round(IFNULL(max(orderno), 0),-1) + 10 FROM ad_column WHERE ad_table_id= #{tableId} and orderno <1000")
    int getMaxOrderNo(long tableId);


    @Update("update ad_column set orderno = #{orderno} where id = #{id}")
    int updateOrderNo(@Param("id") long id, @Param("orderno") long orderno);

    @Update("update ad_column a,(select b.ad_table_id,b.id from ad_column b) b " +
            "set a.ref_table_id=b.ad_table_id " +
            "where a.ref_column_id and b.id=a.ref_column_id and a.ref_column_id is not null and a.id=#{id}")
    int updateRefTable(long id);

    @Select("select dbname, upper(coltype) coltype, nullable, defaultvalue from ad_column where ad_table_id = #{tableId} order by orderno asc")
    List<AdColumnModel> selectByTableId(long tableId);

    @Select("select * FROM ad_column where id=#{id}")
    JSONObject selectFullByid(long id);

    @Select("select FKDISPLAY,AD_LIMITVALUE_GROUP_ID,REF_COLUMN_ID,ORDERNO,COLTYPE,DBNAME,DESCRIPTION,DISPLAYTYPE,INTERPRETER,SUMMETHOD,SEQUENCENAME,MASK,ISAK,ISDK,MODIFIABLE,NULLABLE,OBTAINMANNER FROM ad_column where id=#{id}")
    HashMap selectByid(long id);

    @Select("select ID,NAME FROM ad_table where id=(select ad_table_id from ad_column where id=#{id}) ")
    HashMap selectidBytable(long id);

    @Select("select dbname FROM ad_column where id=#{id}")
    String selectDbNameByTableid(long id);

    @Select("select count(1) from ad_column where ad_table_id = #{tableid} and dbname = #{columnName} limit 1")
    boolean exists(@Param("tableid") long tableid, @Param("columnName") String columnName);

    @Select("select count(1) from ad_column where ad_table_id = #{tableid} and dbname = #{columnName} and id != #{colid} limit 1")
    boolean existsWithoutColId(@Param("tableid") long tableid, @Param("colid") long colid, @Param("columnName") String columnName);
}
